package com.example.myview.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.graphics.withRotation
import androidx.core.graphics.withTranslation
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.example.myview.R
import kotlinx.coroutines.*

class CustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr), LifecycleObserver {
    var mJob: Job? = null
    private var mRadius: Float = 0F
    private var mWidth: Float = 0F
    private var mHeight: Float = 0F
    private var mAngle: Float = 10F
    val mPaintSoild: Paint = Paint()?.apply {
        color = getContext().resources.getColor(R.color.colorWhite)
        style = Paint.Style.STROKE
        isAntiAlias = true
    }
    val mPaintVector: Paint = Paint()?.apply {
        color = getContext().resources.getColor(R.color.colorAccent)
        style = Paint.Style.STROKE
        isAntiAlias = true
    }
    val mPaintCircle: Paint = Paint(Paint.ANTI_ALIAS_FLAG)?.apply {
        color = context.resources.getColor(R.color.colorYellow)
        style = Paint.Style.STROKE
        setPathEffect(DashPathEffect(floatArrayOf(10f, 10f), 0f))
        isAntiAlias = true
    }
    val mPaintText: Paint = Paint().apply {
        textSize = 50f
        typeface = Typeface.DEFAULT_BOLD
        color = ContextCompat.getColor(context,
            R.color.colorWhite
        )
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w.toFloat()
        mHeight = h.toFloat()
        mRadius = if (w / 2 < h / 4) w / 2.toFloat() - 20 else h / 4.toFloat() - 20
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            drawCoordinate(this)
            drawCircle(this)
            drawText(this)
            drawVector(this)
        }
    }

    //画坐标
    private fun drawCoordinate(canvas: Canvas) {
        canvas.withTranslation(mWidth / 2, mHeight / 2) {
            //x 轴
            drawLine(-mWidth / 2, 0f, mWidth / 2, 0f, mPaintSoild)
            //y 轴
            drawLine(0f, -mHeight / 2, 0f, mHeight / 2, mPaintSoild)
        }
        canvas.withTranslation(mWidth / 2, mHeight * 3 / 4) {
            //第二条 x 轴
            drawLine(-mWidth / 2, 0f, mWidth / 2, 0f, mPaintSoild)
        }
    }

    //画圆
    private fun drawCircle(canvas: Canvas) {
        canvas.withTranslation(mWidth / 2, mHeight * 3 / 4) {
            setLayerType(LAYER_TYPE_SOFTWARE, mPaintCircle)
            drawCircle(0f, 0f, mRadius, mPaintCircle)
        }
    }

    private fun drawText(canvas: Canvas) {
        canvas.apply {
            drawRect(10f, 110f, 500f, 240f, mPaintSoild)
            drawText("指数函数与旋转矢量", 25f, 195f, mPaintText)
        }
    }

    private fun drawVector(canvas: Canvas) {
        canvas.withTranslation(mWidth / 2, mHeight * 3 / 4) {
            withRotation(-mAngle) {
                drawLine(0f, 0f, 0f, mRadius, mPaintVector)
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun startRotating() {
        mJob = CoroutineScope(Dispatchers.Main).launch {
            while (true) {
                delay(100)
                mAngle += 5f
                invalidate()
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun pauseRotating() {
        mJob?.cancel()
    }
}